package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Exam;
import com.han.bom.ExamResponse;
import com.han.bom.Professor;
import com.han.bom.RealExam;
import com.han.bom.Student;
import com.han.dao.ExamDAO;
import com.han.dao.ExamResponseDAO;
import com.han.dao.UserDAO;

/**
 * Servlet implementation class ChangeExamStatusServlet
 */
@WebServlet("/Professor/ChangeExamStatusServlet")
public class ChangeExamStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(ChangeExamStatusServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangeExamStatusServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered ChangeExamStatusServlet POST");
		
		Professor professor = null;
		String examIDs=null,mode=null;
		HttpSession session = request.getSession();
		ExamDAO examdao = new ExamDAO();
		UserDAO userdao = new UserDAO();
		
		if(session.getAttribute("professor")!=null){
			professor = (Professor) session.getAttribute("professor");
		}
		else{
			
		}
		
		if(request.getParameter("examIDs")!=null){
			examIDs = request.getParameter("examIDs");
		}
		else{
			
		}
		
		if(request.getParameter("mode")!=null){
			mode = request.getParameter("mode");
		}
		else{
			
		}
		
		ArrayList<Exam> exams = new ArrayList<Exam>();
		
		for(String id:examIDs.split(",")){
			try {
				exams.add(examdao.retrieveExamById(id));
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		}
		
		
		if("Activate".equals(mode)){
			
			try {
				for(Exam exam:exams){
					if(exam instanceof RealExam){
						
						if( !((RealExam)exam).isActivated()){
							examdao.updateActivateCount(exam.getExamID());
							((RealExam)exam).setActivated(true);
							exam.setDraft(false);
						}
						
					}
				}
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
		}
		else if("Deactivate".equals(mode)){
			for(Exam exam:exams){
				((RealExam)exam).setActivated(false);
				
				try {
					ArrayList<Student> students = userdao.retrieveStudentsTakingExam(exam.getExamID());

					
					
					ExamResponseDAO examresponsedao = new ExamResponseDAO();
					for(Student student: students){
						ArrayList<ExamResponse> blankresponse = new ArrayList<ExamResponse>();
						blankresponse.add(new ExamResponse(exam, student.getUserID(), null));
					
						student.setExamAnswers(blankresponse);
						examdao.removeStudentFromExam(student.getUserID(), exam.getExamID());
						
						examresponsedao.insertExamResponse(student,false);
					}
				} catch (ClassNotFoundException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (SQLException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
				
				
			}
			
			
			
		}
		else{
			
		}
		
		for(Exam exam:exams){
			
			try {
				examdao.updateExam(exam);
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		
		}
		
		response.sendRedirect("ProfessorViewExamServlet?type=Real");
		
	}

}
